#include "barcodedata.h"
#include "log4z.h"
#include "rs232.h"
#include <QThread>
#include <QTextCodec>

BarcodeData::BarcodeData(QObject *parent)
{
    bRunning = true;
}

BarcodeData::~BarcodeData()
{

}

void BarcodeData::stopRead()
{
    bRunning = false;
}

void BarcodeData::run()
{
    int cport_nr =16;
    LOGFMTD("Func %s BarcodeData", __func__);
    //read data
    while (bRunning)
    {
        unsigned char readBuf[128];
        memset(readBuf, 0, 128);
        int iCnt = RS232_PollComport(cport_nr, readBuf, 128);
        if (iCnt > 0)
        {
            std::string strBuf(reinterpret_cast<const char*>(readBuf), iCnt);
            LOGFMTD("Func %s: Size=%d, Buff=%s", __func__, iCnt, strBuf.c_str());
            for (int i=0; i<8; i++)
            {
                LOGFMTD("hex %x", readBuf[i]);
            }
            emit readData(strBuf.c_str());
        }
        else
        {
            QThread::msleep(500);
        }
    }
}
